//
//  BookTopicsDetailSecondCell.m
//  FreeTrip
//
//  Created by 352324497@qq.com on 15/11/27.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "BookTopicsDetailSecondCell.h"

@implementation BookTopicsDetailSecondCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.selectionStyle = NO;
        self.lineLabel = [[UILabel alloc]init];
        self.lineLabel.backgroundColor = [UIColor blackColor];
        [self.contentView addSubview:_lineLabel];
        
        self.titleLabel = [[UILabel alloc]init];
        self.titleLabel.font = [UIFont systemFontOfSize:20 * KScreenWidth / 375];
        [self.contentView addSubview:_titleLabel];
        
        self.myDescription = [[UILabel alloc]init];
        self.myDescription.width = KScreenWidth - 20 * KScreenWidth / 375;
        self.myDescription.numberOfLines = 0;
        self.myDescription.font = [UIFont systemFontOfSize:16 * KScreenWidth / 375];
        [self.contentView addSubview:_myDescription];
        
        self.photoImageView  = [[UIImageView alloc]init];
        [self.contentView addSubview:_photoImageView];
        
        self.myDescriptionLabel = [[UILabel alloc]init];
        self.myDescriptionLabel.width = KScreenWidth - 20 * KScreenWidth / 375;
        self.myDescriptionLabel.numberOfLines = 0;
        self.myDescriptionLabel.font = [UIFont systemFontOfSize:16 * KScreenWidth / 375];
        [self.contentView addSubview:_myDescriptionLabel];
        
        self.placeButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
        self.placeButton.titleLabel.textAlignment = NSTextAlignmentLeft;
//        self.placeButton.layer.borderWidth = 0.2;
//        self.placeButton.layer.borderColor = [UIColor grayColor].CGColor;
        [self.contentView addSubview:_placeButton];
    }
    return self;
}

-(void)setModel:(BookTopicsDetailModel *)model
{
    CGFloat x = 10 * KScreenWidth / 375;
    _model = model;
    self.placeButton.frame = CGRectMake(0, 0, 0, 0);
    self.titleLabel.text = @"";
    self.myDescription.text = @"";
    self.myDescriptionLabel.text = @"";
    self.placeButton.titleLabel.text = nil;
    
    if (model.title.length != 0 && model.image_url.length == 0)
    {
        self.lineLabel.frame = CGRectMake(x , x + 5 * KScreenWidth / 375, 2, 2 * x);
        self.titleLabel.frame = CGRectMake(2 * x, x, KScreenWidth - 4 * x, 3 * x);
        self.titleLabel.text = model.title;
        self.myDescription.text = model.myDescription;
        CGRect bounds = [model.myDescription boundingRectWithSize:CGSizeMake(KScreenWidth - x * 2, 10000 * KScreenWidth / 375) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16 * KScreenWidth / 375] forKey:NSFontAttributeName] context:nil];
        self.myDescription.frame = CGRectMake(x, 5 * x, KScreenWidth - 2 * x, bounds.size.height + 5);
        self.photoImageView.height = 0;
        self.myDescriptionLabel.height = 0;
        if (model.name == nil)
        {
            self.placeButton.height = 0;
        }
        
    }
    else if (model.image_width != 0)
    {
        self.lineLabel.height = 0;
        self.myDescription.height = 0;
        CGFloat height = KScreenWidth * model.image_height / model.image_width;
        self.photoImageView.frame = CGRectMake(0, 10 * KScreenWidth / 375, KScreenWidth, height);
        [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:model.image_url] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
        self.myDescriptionLabel.text = model.myDescription;
        
        CGRect bounds = [model.myDescription boundingRectWithSize:CGSizeMake(KScreenWidth - 20 * KScreenWidth / 375, 10000 * KScreenWidth / 375) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16 * KScreenWidth / 375] forKey:NSFontAttributeName] context:nil];
        self.myDescriptionLabel.frame = CGRectMake(10 * KScreenWidth / 375, height + 20 * KScreenWidth / 375, KScreenWidth - 20 * KScreenWidth / 375, bounds.size.height + 5);
        if (model.name != nil)
        {
            self.placeButton.frame = CGRectMake(20 * KScreenWidth / 375, height + bounds.size.height + 40 * KScreenWidth / 375, KScreenWidth - 40 * KScreenWidth / 375, 20 * KScreenWidth / 375);
            [self.placeButton setTitle:model.name forState:(UIControlStateNormal)];
            
        }
    }
}

+(CGFloat)heightForCell:(BookTopicsDetailModel *)model
{
    CGFloat x = 10 * KScreenWidth / 375;
    if (model.title.length != 0 && model.image_height == 0)
    {
        CGRect bounds = [model.myDescription boundingRectWithSize:CGSizeMake(KScreenWidth - x * 2, 10000 * KScreenWidth / 375) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16 * KScreenWidth / 375] forKey:NSFontAttributeName] context:nil];
        return 60 * KScreenWidth / 375 + bounds.size.height;
    }
    else if (model.image_width != 0)
    {
        CGFloat height = KScreenWidth * model.image_height / model.image_width;
        CGRect bounds = [model.myDescription boundingRectWithSize:CGSizeMake(KScreenWidth - 20 * KScreenWidth / 375, 10000 * KScreenWidth / 375) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16 * KScreenWidth / 375] forKey:NSFontAttributeName] context:nil];
        if (model.name == nil)
        {
            return 40 * KScreenWidth / 375 + height + bounds.size.height;
        }
        return 70 * KScreenWidth / 375 + height + bounds.size.height;
    }
    return 600;

}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
